package com.hdcx.Minfresh.adapter;


import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.bean.GoodSortWrapper;
import com.hdcx.Minfresh.fragment.ClassifyFragment;
import com.hdcx.Minfresh.holder.ClassifyHolder;

/**
 * Created by Administrator on 2016/11/9.
 */

public class ClassifyLeftAdapter extends RecyclerView.Adapter<ClassifyHolder> {
    FragmentActivity mActivity;
    public int mPosition = 0;
    GoodSortWrapper data;
    ClassifyFragment fragment;


    public ClassifyLeftAdapter(FragmentActivity mActivity, ClassifyFragment classifyFragment) {
        this.mActivity = mActivity;
        fragment= classifyFragment;
    }

    @Override
    public ClassifyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        View view;
            view= layoutInflater.inflate(R.layout.item_fragment_classify_one_, parent, false);
        return  new ClassifyHolder(view,viewType);
    }

    @Override
    public void onBindViewHolder(ClassifyHolder holder, final int position) {
          holder.tv_name.setText(data.getList().get(position).getName());
        if(mPosition == position){
            holder.line.setVisibility(View.VISIBLE);
            holder.layout_all.setBackgroundColor(mActivity.getResources().getColor(R.color.white));
            holder.img_ic.setImageResource(R.drawable.fowordarrow);
        }else{
            holder.line.setVisibility(View.INVISIBLE);
            holder.layout_all.setBackgroundColor(mActivity.getResources().getColor(R.color.line3));
            holder.img_ic.setImageResource(R.drawable.downarrow);
            holder.layout_all.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("position", "onClick: "+position +"mPosition"+mPosition);
                    mPosition = position;
                    fragment.onMyItemClick(mPosition);
                    notifyDataSetChanged();
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if(data==null || data.getList()==null){
            return 0;
        }
        return data.getList().size();
    }

    public void setData(GoodSortWrapper response) {
        data = response;
        notifyDataSetChanged();

    }
    public void setmPosition(String id){
        if(data!=null && data.getList()!=null){
            for(int i=0;i<data.getList().size();i++){
                if(data.getList().get(i).getId().equals(id)){
                    mPosition=i;
                    fragment.onMyItemClick(mPosition);
                    notifyDataSetChanged();
                    return;
                }
            }
        }
    }

}
